import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
import {map} from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class TestService {

  private finished$ = new Subject<string>();
  notify$;
  constructor() {
    this.notify$ = this.finished$.pipe(map(status => status));
  }

  list = [];

  add(uid: symbol): void {
    const i = this.list.findIndex(rec => rec.uid === uid);
    if (i === -1) {
      this.list.push({uid, finished: false});
    }
  }
  done(uid: symbol): void {
    const r = this.list.find(rec => rec.uid === uid);
    if (r) {
      r.finished = true;
    }
    console.log('done', this.list, uid);
    this.checkAllComplete();
  }

  private checkAllComplete(): void {
    const r = this.list.find(rec => !rec.finished);
    if (!r) {
      this.finished$.next('finished');
    }
  }
}
